package com.expense.app.admin.controller;

import cn.hutool.core.util.StrUtil;
import com.expense.app.admin.annotation.IgnoreAuth;
import com.expense.app.admin.model.params.job.JobImportParam;
import com.expense.app.admin.model.params.job.ibm.JobListParam;
import com.expense.app.admin.model.params.job.ibm.JobParam;
import com.expense.app.admin.services.job.IBMJobServ;
import com.expense.common.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;
import java.net.URLDecoder;

/*
* 中控 集团 校招 管理 接口
* gen controller 2019-08-20
*/
@Validated
@RestController
@RequestMapping("/ibm/job")
public class IBMJobCtrl {

    @Autowired
    IBMJobServ ibmJobServ;

    @PostMapping("/import")
    public Result importExcel(@RequestBody JobImportParam param) {
        return Result.success(ibmJobServ.importExcel(param));
    }

    @GetMapping("/list")
    public Result findList(@Validated JobListParam param) {
        return Result.success(ibmJobServ.findList(param));
    }

    @IgnoreAuth
    @GetMapping("/web/list")
    public Result findWebList(@Validated JobListParam param) {
        try{
            String key = param.getKey();
            if(StrUtil.isNotBlank(key)){
                param.setKey(URLDecoder.decode(key,"UTF-8"));
            }
            String city = param.getCity();
            if(StrUtil.isNotBlank(city)){
                param.setCity(URLDecoder.decode(city,"UTF-8"));
            }
            String type = param.getJobType();
            if(StrUtil.isNotBlank(type)){
                param.setJobType(URLDecoder.decode(type,"UTF-8"));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return Result.success(ibmJobServ.findList(param));
    }

    @IgnoreAuth
    @PostMapping("/web/list")
    public Result findWebLists(@Validated @RequestBody JobListParam param) {
        return Result.success(ibmJobServ.findList(param));
    }

    @IgnoreAuth
    @GetMapping("/web/list/{id}")
    public Result findJobDetail(@PathVariable @Min(1) Long id) {
        return Result.success(ibmJobServ.findDetail(id));
    }

    @IgnoreAuth
    @GetMapping("/web/init")
    public Result findInitList() {
        return Result.success(ibmJobServ.findInitList());
    }

    @GetMapping(value = "/{id}")
    public Result findDetail (@PathVariable @Min(1) Long id) {
        return Result.success(ibmJobServ.findDetail(id));
    }

    @PostMapping(value = "")
    public Result saveOne(@Validated @RequestBody JobParam param) {
        return Result.success(ibmJobServ.saveOne(param));
    }

    @PutMapping(value = "")
    public Result update(@Validated @RequestBody JobParam param) {
        return Result.success(ibmJobServ.saveOne(param));
    }

    @DeleteMapping(value = "/{id}")
    public Result deleteOne(@PathVariable @Min(1) Long id) {
        return Result.success(ibmJobServ.deleteOne(id));
    }
}
